package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Adapters.AddEvaluationlistviewAdapter;
import com.example.starseas_phone.Adapters.Tea_AllStudentslistviewAdapter;
import com.example.starseas_phone.Entity.AddEvaluation;
import com.example.starseas_phone.Entity.Students;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Tea_AllStudentsActivity extends AppCompatActivity {

    private TextView tea_allstudents_title;

    private ListView tea_all_students_list;
    private Tea_AllStudentslistviewAdapter adapter;
    private List<Students> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tea_all_students);
        initid();
        initview();
        tea_all_students_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Students students = adapter.getItem(i);
                Intent intent = new Intent(Tea_AllStudentsActivity.this,AddStu_detailsActivity.class);
                intent.putExtra("students",students);
                intent.putExtra("str","学生详细信息");
                startActivity(intent);
            }
        });
    }

    private void initid(){
        tea_allstudents_title = (TextView) findViewById(R.id.tea_allstudents_title);
        tea_allstudents_title.setText(Sto.teachers.getClassId()+"班成员信息");
        tea_all_students_list = (ListView) findViewById(R.id.tea_all_students_list);
        adapter = new Tea_AllStudentslistviewAdapter(Tea_AllStudentsActivity.this,R.layout.tea_all_students_listview,list);
    }

    private void initview(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Student/GetStudentByClassId?token="+Sto.teachers.getToken())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObjects = new JSONObject(responseString);
                    String msg = jsonObjects.getString("message");
                    if (msg.equals("该老师无学生")){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Tea_AllStudentsActivity.this,msg,Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else {
                        JSONArray jsonArray = new JSONArray(msg);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String StuName = jsonObject.getString("StuName");
                            int StuAge = jsonObject.getInt("StuAge");
                            String StuSex = jsonObject.getString("StuSex");
                            String StuPhone = jsonObject.getString("StuPhone");
                            String StuEmail = jsonObject.getString("StuEmail");
                            String StuCarid = jsonObject.getString("StuCarid");
                            String StuClassId = jsonObject.getString("ClassId");
                            String StubackTime = jsonObject.getString("StubackTime");
                            String PoliticalLandscape = jsonObject.getString("PoliticalLandscape");
                            String NativePlace = jsonObject.getString("NativePlace");
                            String National = jsonObject.getString("National");
                            String StateOfTheSchool = jsonObject.getString("StateOfTheSchool");
                            Students students = new Students(StuName,StuAge,StuSex,StuPhone,StuEmail,StuCarid,StuClassId,StubackTime,PoliticalLandscape,NativePlace,National,StateOfTheSchool);
                            list.add(students);
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                                tea_all_students_list.setAdapter(adapter);
                            }
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}